define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
        require("jquery_ui");
    var imageTemp = require("./template/imageTemp.html");
    var progressTemp=require("./template/progress.html");
    var render_item = template.compile(imageTemp);
    var render_progress = template.compile(progressTemp);
    var uploadimte=null; 
    $(".js_new_upload").each(function(){
        var $el=$(this),$form=$el.closest('.js_upload_container'),sname=$el.data("submitname"),
        upload_path=$el.data("uploadpath"),deletepath=$el.data("delpath"),count=$el.data("count"),datas=eval('(' + $el.data("data") + ')')||null;
        var max_count=count-($form.find("li.imgbox").length || 0);
        if(count==undefined || count==""){
            max_count=null;
        }
        var cc=$el.uploader({
            multi: true,
            url: upload_path,
            delete_path:deletepath,
            max_count:max_count,
            max_count_tips:count,
            data:datas,
            FilesAdded: function(up, files) {
                $.each(files, function(index, file) {
                    //var m = method.getFormatDateStrNow();
                   $(".js_file_upload_queue",$form).append(render_progress({
                        id:file.id,
                        filename:file.name,
                        filesize:Math.ceil(file.size/1024)
                    }));

                });
            },
            UploadProgress: function(up, file) {
                var upfileid = "upload{0}".format(file.id);
                uploadimte = $("#{0}".format(upfileid));
                if (uploadimte.length) {
                    var percent = file.percent + "%";
                    $(".data", uploadimte).text(percent)
                    $(".uploadify-progress-bar", uploadimte).width(percent)
                };

            },
            FileUploaded: function(up, file, res) { 
                if ($.isPlainObject(res)) {
                     res.image.progressid=file.id;
                     res.image.sname=sname;
                     res.image.deletepath=deletepath;
                     res.image.index=$form.find("li.imgbox").length;
                    $(".js_fileList",$form).append(render_item(res.image));
                    uploadimte.remove();
                } else {
                    uploadimte.addClass('uploadify-error');
                    uploadimte.remove();
                    up.files.removeFile(file);
                    config.msg.info(res || config.lang.uplodError);
                } 
            },
            UploadComplete: function(up, files) {
               $(".uploadify-progress-bar", uploadimte).remove();
            }
        }, "picture");
        $("ul.ipost-list",$form).sortable({
            opacity: .8
        });
        var $del=$("a.item_new_close",$form);
        $form.on("click", "a.item_new_close", function(e) {
            var t = $(this),progressid=t.data("progressid"),delpath=t.data("delpath");
            $.post(delpath, {
                id: t.data("post-id"),
                url: t.data("path")
            });
            var $box = $(e.target).closest("li.imgbox");
            $box.fadeOut(function() {
                $box.remove();
                $("#upload"+progressid).remove();
                var maxcount=count-($form.find("li.imgbox").length || 0),files=0;
                $form.find("li.imgbox").each(function(){
                    if($(this).find("a.item_new_close").data("progressid")){
                        files++;
                    }
                });
                if(count){
                    if(progressid){
                        $.data($el, 'uploader').plupload.removeFile(progressid)
                    }else{
                        $el.uploader("update",{max_count:maxcount+files})
                    }
                }
                
            });
        });
    });
});